import * as vscode from "vscode";
import { join, dirname, normalize } from "path";
import { readFileSync } from "fs";

export class ComponentCustomHoverProvider implements vscode.HoverProvider {
  provideHover(document: vscode.TextDocument, position: vscode.Position) {
    const fileName = document.fileName;
    const workDir = dirname(fileName);
    const word = document.getText(document.getWordRangeAtPosition(position));
    const line: any = document.lineAt(position);

    if (!line.text.includes("Custom") || !word) {
      return;
    }

    let workspacePath = "";
    if (vscode.workspace.workspaceFolders?.length) {
      workspacePath = vscode.workspace.workspaceFolders[0].uri.fsPath;
      workspacePath = normalize(workspacePath);
    }

    const cwd = workspacePath;
    let ProviderPath: string = "";
    let FormProviderPath: string = "";

    try {
      const components: any[] = [];
      // 获取用户设置
      const configurate = vscode.workspace
        .getConfiguration()
        .get("vh-tips-tool.type");

      let uri = "";
      if (configurate === "vh-admin") {
        ProviderPath = join(
          cwd,
          "node_modules",
          "@vh-admin",
          "pro-components",
          "es",
          "index.d.ts"
        );

        FormProviderPath = join(
          cwd,
          "node_modules",
          "@vh-admin",
          "pro-components",
          "es",
          "f-form-custom",
          "components.d.ts"
        );

        uri = "http://vh-admin.poetrysl.com/pro-components/";

        components.push({
          key: "ModalTypeRenderConfig",
          url: uri,
        });
      } else {
        uri = "http://vh-mobile.poetrysl.com/pro-components/";

        ProviderPath = join(
          cwd,
          "node_modules",
          "@vh-mobile",
          "pro-components",
          "es",
          "index.d.ts"
        );

        FormProviderPath = join(
          cwd,
          "node_modules",
          "@vh-mobile",
          "pro-components",
          "es",
          "f-form-custom",
          "components.d.ts"
        );
      }

      const ProviderFile = readFileSync(ProviderPath).toString();
      const FormProviderFile = readFileSync(FormProviderPath).toString();

      const numRegex = new RegExp("export.*Custom.*;", "g");
      const match: any = ProviderFile.match(numRegex);
      match.forEach((i: any) => {
        const sArr = i.split("from");
        if (sArr.length > 1) {
          const str1 = sArr[0]
            .replace("import", "")
            .replace("export", "")
            .replace("as", "")
            .replace("default", "")
            .replace("{", "")
            .replace("}", "")
            .trim();
          const str2 = sArr[1]
            .replace(/\'/g, "")
            .replace(/\./g, "")
            .replace(/\//g, "")
            .replace(/;/g, "")
            .trim();
          components.push({
            key: str1,
            url: `${uri}${str2}`,
          });
        }
      });

      const numRegexForm = new RegExp("import.*Custom.*;", "g");
      const matchForm: any = FormProviderFile.match(numRegexForm);
      matchForm.forEach((i: any) => {
        const sArr = i.split("from");
        if (sArr.length > 1) {
          const str1 = sArr[0]
            .replace("import", "")
            .replace("export", "")
            .replace("as", "")
            .replace("default", "")
            .replace("{", "")
            .replace("}", "")
            .trim();
          const str2 = sArr[1]
            .replace(/\'/g, "")
            .replace(/\./g, "")
            .replace(/\//g, "")
            .replace(/;/g, "")
            .trim();
          components.push({
            key: str1,
            url: `${uri}${str2}`,
          });
        }
      });
     
      const has = components.find((i: any) => i.key === word);

      if (!has) {
        return;
      }

      return new vscode.Hover(`#### 组件：${word}
点击查看文档：[${word}](${has.url})
`);
    } catch (error) {
      console.log("===================");
      console.log(error);
      console.log("===================");
    }
  }
}
